import { STORAGE_SIDEBAR_OPEN } from '@/common/plugins/constant'

const state = {
  sidebar: {
    collapse: false,
    visible: true,
  },
  loading: false,
}

const getters = {
  sidebar: state => state.sidebar,
  loading: state => state.loading,
}

const actions = {
  toggleSidebarCollapse({ commit }, collapse) {
    commit('toggleSidebarCollapse', collapse)
  },
  toggleSidebarVisible({ commit }, visible) {
    commit('toggleSidebarVisible', visible)
  },
}

const mutations = {
  toggleSidebarCollapse: (state, payload) => {
    state.sidebar.collapse = payload === undefined ? !state.sidebar.collapse : payload
    localStorage.setItem(STORAGE_SIDEBAR_OPEN, state.sidebar.collapse)
  },
  toggleSidebarVisible: (state, payload) => {
    state.sidebar.visible = payload
  },
  toggleLoading: (state, payload) => {
    state.loading = payload
  },
}

export default {
  state,
  getters,
  actions,
  mutations,
}
